package com.lottery.service.qiuqiu.lobbyService;

import com.lottery.controller.BaseController;
import com.lottery.dao.qiuqiu.accoutDao.UserDao;
import com.lottery.dao.qiuqiu.lobbyDao.BettinginfoDao;
import com.lottery.dao.qiuqiu.lobbyDao.LotteryDao;
import com.lottery.dao.qiuqiu.lobbyDao.LotteryExchangeDao;
import com.lottery.dao.qiuqiu.wealthDao.AccountDao;
import com.lottery.dao.qiuqiu.wealthDao.AccountTransactionDao;
import com.lottery.dto.*;
import com.lottery.entity.*;
import com.lottery.initData.InitData;
import com.lottery.service.qiuqiu.JedisClient;
import com.lottery.utils.DateUtil;
import com.lottery.utils.DealWithDouble;
import com.lottery.utils.lobbyUtils.impl.BjscPlayUtilImpl;
import com.lottery.utils.lobbyUtils.impl.SscPlayutilImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LobbyService extends BaseController {


    // ssc下一次开奖的时间
    @Value("${SSCNEXTTIME}")
    private String SSCNEXTTIME;

    // ssc下一次开奖的期号
    @Value("${SSCNEXTEXPECT}")
    private String SSCNEXTEXPECT;

    // ssc本次开奖的期号
    @Value("${SSCEXPECT}")
    private String SSCEXPECT;


    //北京赛车相关
    @Value("${BJSCEXPECT}")
    private String BJSCEXPECT;
    @Value("${BJSCNEXTTIME}")
    private String BJSCNEXTTIME;
    @Value("${BJSCNEXTEXPECT}")
    private String BJSCNEXTEXPECT;


    // 每个用户每期最高下注金额
    @Value("${MAXMONEY}")
    private String MAXMONEY;

    @Resource
    private BettinginfoDao bettinginfoDao;
    @Resource
    private LotteryDao lotteryDao;
    @Resource
    private LotteryExchangeDao lotteryExchangeDao;
    @Resource
    private UserDao userDao;
    @Resource
    private SscPlayutilImpl sscPlayutil;

    @Resource
    private BjscPlayUtilImpl bjscPlayUtil;
    @Resource
    private AccountDao accountDao;

    @Resource
    private JedisClient jedis;
    @Resource
    private AccountTransactionDao accountTransactionDao;

    public void searchBetRecord(String startTime, String endTime, String userId, ResultDto result) {
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("userId", userId);

        List<String> expectList = bettinginfoDao.selectBetRecordDay(params);
        if (expectList.size() > 0) {
            //初始化删选map
            LinkedHashMap<String,BetDto> initMap = new LinkedHashMap<>();
            expectList.forEach((k)->{
                BetDto bt = new BetDto();
                initMap.put(k,bt);
            });

            List<BetDto> list = bettinginfoDao.searchBetRecord(params);
            List<BetDto> betDtoList = new ArrayList<>();
            list.forEach((k)->{
                BetDto dto = initMap.get(k.getExpect());
                dto.setExpect(k.getExpect());
                dto.setLotteyType(k.getLotteyType());
                dto.setBetMoney(DealWithDouble.sum(dto.getBetMoney(),k.getBetMoney(),2,4));
                if(k.getState().equals("1")){
                    dto.setWinTotal(DealWithDouble.sum(dto.getWinTotal(),k.getWinMoney(),4,2));
                    dto.setWinMoney(DealWithDouble.sub(dto.getWinTotal(),k.getBetMoney(),2,4));
                }
                double profit = DealWithDouble.sub(k.getWinMoney(), k.getBetMoney(), 2, 4);
                dto.setProfit(DealWithDouble.sum(dto.getProfit(),profit,2,4));
            });


//            for (int i = 0; i < length; i++) {
//
//
//                BetDto aa = list.get(i);
//                bet = new BetDto();
//                bet.setBetMoney(DealWithDouble.sum(0, aa.getBetMoney(), 2, 4));
//                bet.setLotteyType(aa.getLotteyType());
//                bet.setExpect(aa.getExpect());
//                bet.setWinMoney(DealWithDouble.sum(aa.getWinMoney(), 0, 2, 4));
//                bet.setWinTotal(DealWithDouble.sub(aa.getWinMoney(), bet.getWinMoney(), 2, 4));
//                bet.setProfit(DealWithDouble.sub(bet.getWinMoney(), bet.getBetMoney(), 2, 4));
//                betDtoList.add(bet);
//                betTotal = 0;
//
//            }

            initMap.forEach((k,v)->{
                betDtoList.add(v);
            });
            Map<String, Object> map = new HashMap<>();
            map.put("recordTotal", betDtoList.size());
            //统计数据和
            double betTol = 0;
            double win1 = 0; //不含本
            double win2 = 0; //含本
            double profitTotal = 0;


            for (BetDto dd : betDtoList) {
                betTol = DealWithDouble.sum(betTol, dd.getBetMoney(), 2, 4);
                win1 = DealWithDouble.sum(win1, dd.getWinMoney(), 2, 4);
                win2 = DealWithDouble.sum(win2, dd.getWinTotal(), 2, 4);
                profitTotal = DealWithDouble.sum(profitTotal, dd.getProfit(), 2, 4);
            }
            map.put("betTotal", betTol);
            map.put("winExpectBet", win1);
            map.put("winTotal", win2);
            map.put("profit", profitTotal);
            result.setData(map);
            result.setResultData(true, "查询信息成功", 0, betDtoList);
        } else {
            result.setResultData(false, "no date", 2, null);
        }

    }

    public void searchWealthInfo(int userId, String expect, int lotteyType, String agencyMark, ResultDto result) throws Exception {
        //查询用户信息
        User user = userDao.selectId(userId);

        //查询开奖详情
        Lottery lottery = lotteryDao.selectLotteryDetail(expect, lotteyType);
        //拆分开奖结果
        Map<String, Object> map = new HashMap<>();
        String winResult = lottery.getWinInfo();
        if (winResult != null && !("".equals(winResult))) {
            String[] strings = winResult.split("&&");
            map.put("winInfoBefore", strings[0]);
            map.put("winInfoAfter", strings[1]);
        } else {
            result.setResultData(false, "拉取失败", 2, null);
            logger.error("******LobbyService searchWealthInfo database winInfo data error ******");
            return;
        }

        map.put("lotteyType", lotteyType);
        map.put("expect", expect);
        map.put("opencode", lottery.getOpencode());
        map.put("opentime", DateUtil.getFormatDate(lottery.getOpentime(), "YYYY-MM-dd hh:mm:ss"));
        result.setData(map);

        //查询当期的下注金额
        Map<String, Object> params = new HashMap<>();
        params.put("expect", expect);
        params.put("lotteyType", lotteyType);
        params.put("userId", userId);
        List<Bettinginfo> bettinginfos = bettinginfoDao.selectBettinginfoForUser(params);
        List<BetResultDto> resultDtoList = new ArrayList<>();
        BetResultDto bet = null;

        //查询当期的中奖金额
        List<LotteryExchange> lotteryExchanges = lotteryExchangeDao.selectLotteryExchangeListForUser(params);
        Map<Integer, Double> judgeWin = new HashMap<>();
        boolean flag = true; //该用户这期中奖标志
        if (lotteryExchanges.size() > 0) {
            for (LotteryExchange ee : lotteryExchanges) {
                judgeWin.put(ee.getBettinginfoId(), ee.getWinningamount());
            }
        } else {
            flag = false;
        }

        //更新数据信息
        for (Bettinginfo bt : bettinginfos) {
            bet = new BetResultDto();
            bet.setBetMoney(bt.getBettingmoney());
            bet.setBetType(bt.getBettingrule());
            bet.setBetResult(bt.getState());//0:中奖,1:未中奖,2:未开奖
            if (flag) {
                Integer id = bt.getId();
                if (judgeWin.containsKey(id)) {
                    double sum = DealWithDouble.sum(0 , judgeWin.get(id), 2, 4);
                    bet.setWinMoney(sum);
                }
            } else {
                bet.setWinMoney(0);
            }
            String bettingrule = null;
            if (lotteyType == 1) {
                bettingrule = SscPlayutilImpl.getSscBetOdd(bt.getPlaywayinfoId(), bt.getBettingrule());
            } else if (lotteyType == 3) {
                bettingrule = BjscPlayUtilImpl.getSscBetOdd(bt.getPlaywayinfoId(), bt.getBettingrule());
            }
           /* if (bt.getPlaywayinfoId() == 7) {
                bettingrule = "1";
            }*/
            double odd = InitData.LOTTEY_REF_LOTTEYODD.get(lotteyType).get(agencyMark).get(bt.getPlaywayinfoId()).get(bettingrule).getOdds();
            bet.setLotteyOdd(odd);
            resultDtoList.add(bet);
        }

        result.setResultData(true, "拉取信息成功", 0, resultDtoList);
    }


    public void Sscplayway(Integer userId, Integer lotteyType, String agencyMark, List<PlaywayDto> list, Integer loginType, ResultDto result) throws Exception {
        //先决条件判断
        BetConditionDto bto = new BetConditionDto(userId, agencyMark, result, lotteyType, list);
        sscPlayutil.getInstance(bto, loginType);
        if (result.isSuccess()) {
            if (loginType == 0) {
                sscPlayutil.insertBetData(list, userId, lotteyType,agencyMark, result);
            } else {
                sscPlayutil.insertTourBetData(list, userId, lotteyType,agencyMark, result);
            }

        }
    }


    public void searchOddByAgency(String agencyMark, Integer lotteyType, ResultDto result) throws Exception {
        //根据渠道商Id获取赔率
        Map<Integer, LinkedHashMap<String, LotteryOdds>> oddsMap = InitData.LOTTEY_REF_LOTTEYODD.get(lotteyType).get(agencyMark);
        List<Map<String, Object>> list = new ArrayList<>();
        oddsMap.forEach((k, v) -> {
            //封装数据
            if (lotteyType == 1) {
                init(k, v, list);
            } else if (lotteyType == 3) {
                initBjsc(k, v, list);
            }
        });
        result.setData2(lotteyType);
        result.setResultData(true, "获取赔率信息成功", 0, list);
    }

    private void initBjsc(Integer k, LinkedHashMap<String, LotteryOdds> v, List<Map<String, Object>> list) {
        switch (k) {
            case 10:
            case 12:
            case 13:
            case 15:
            case 16:
                Map<String, Object> daXiaoMap = new HashMap<>();
                if (v.size() > 0) {
                    List<OddRefAgencyDto> listaa = new ArrayList<>();
                    v.forEach((key, value) -> {
                        listaa.add(new OddRefAgencyDto(key, "", value.getOdds()));
                    });
                    daXiaoMap.put("playinfoId", k);
                    daXiaoMap.put("flag", true);
                    daXiaoMap.put("list", listaa);
                } else {
                    daXiaoMap.put("playinfoId", k);
                    daXiaoMap.put("flag", false);
                }
                list.add(daXiaoMap);
                break;
            case 11:
                Map<String, Object> caiChehaoMap = new HashMap<>();
                if (v.size() > 0) {
                    List<OddRefAgencyDto> listaa = new ArrayList<>();
                    v.forEach((key, value) -> {
                        listaa.add(new OddRefAgencyDto(key + "号", "", value.getOdds()));
                    });
                    caiChehaoMap.put("playinfoId", k);
                    caiChehaoMap.put("flag", true);
                    caiChehaoMap.put("list", listaa);
                } else {
                    caiChehaoMap.put("playinfoId", k);
                    caiChehaoMap.put("flag", false);
                }
                list.add(caiChehaoMap);
                break;
            case 14:
                Map<String, Object> guanyaMap = new HashMap<>();
                if (v.size() > 0) {
                    List<OddRefAgencyDto> listaa = new ArrayList<>();
                    v.forEach((key, value) -> {
                        for (int i = 1; i < 10; i++) {
                            listaa.add(new OddRefAgencyDto("0" + i, "", value.getOdds()));
                        }
                        listaa.add(new OddRefAgencyDto("10", "", value.getOdds()));
                    });
                    guanyaMap.put("playinfoId", k);
                    guanyaMap.put("flag", true);
                    guanyaMap.put("list", listaa);
                } else {
                    guanyaMap.put("playinfoId", k);
                    guanyaMap.put("flag", false);
                }
                list.add(guanyaMap);
                break;


        }
    }

    private void init(Integer k, Map<String, LotteryOdds> v, List<Map<String, Object>> list) {
        switch (k) {
            case 4:
                Map<String, Object> daXiaoMap = new HashMap<>();
                if (v.size() > 0) {
                    List<OddRefAgencyDto> listaa = new ArrayList<>();
                    v.forEach((key, value) -> {
                        switch (key) {
                            case "大":
                                listaa.add(new OddRefAgencyDto(key, "5,6,7,8,9", value.getOdds()));
                                break;
                            case "小":
                                listaa.add(new OddRefAgencyDto(key, "0,1,2,3,4", value.getOdds()));
                                break;
                            case "单":
                                listaa.add(new OddRefAgencyDto(key, "1,3,5,7,9", value.getOdds()));
                                break;
                            case "双":
                                listaa.add(new OddRefAgencyDto(key, "0,2,4,6,8", value.getOdds()));
                                break;

                        }
                    });
                    daXiaoMap.put("playinfoId", k);
                    daXiaoMap.put("flag", true);
                    daXiaoMap.put("list", listaa);
                } else {
                    daXiaoMap.put("playinfoId", k);
                    daXiaoMap.put("flag", false);
                }
                list.add(daXiaoMap);
                break;
            case 6:
                Map<String, Object> teMaMap = new HashMap<>();
                if (v.size() > 0) {
                    List<OddRefAgencyDto> listaa = new ArrayList<>();
                    v.forEach((key, value) -> {
                        switch (key) {
                            case "A":
                                listaa.add(new OddRefAgencyDto(key, "0-15", value.getOdds()));
                                break;
                            case "B":
                                listaa.add(new OddRefAgencyDto(key, "16-29", value.getOdds()));
                                break;
                            case "C":
                                listaa.add(new OddRefAgencyDto(key, "30-45", value.getOdds()));
                                break;
                            default:
                                listaa.add(new OddRefAgencyDto(key, "&&", value.getOdds()));
                        }
                    });
                    teMaMap.put("playinfoId", k);
                    teMaMap.put("flag", true);
                    teMaMap.put("list", listaa);
                } else {
                    teMaMap.put("playinfoId", k);
                    teMaMap.put("flag", false);
                }
                list.add(teMaMap);
                break;
            case 7:
                Map<String, Object> danmaMap = new HashMap<>();
                if (v.size() > 4) {
                    List<OddRefAgencyDto> listaa = new ArrayList<>();
                    for (int i = 0; i < 10; i++) {
                        listaa.add(new OddRefAgencyDto(i + ""));
                    }
                    danmaMap.put("playinfoId", k);
                    danmaMap.put("flag", true);
                    danmaMap.put("list", listaa);

                } else {
                    danmaMap.put("playinfoId", k);
                    danmaMap.put("flag", false);
                }
                list.add(danmaMap);
                break;
            case 5:
            case 8:
            case 9:
                Map<String, Object> otherMap = new HashMap<>();
                if (v.size() > 0) {
                    List<OddRefAgencyDto> listaa = new ArrayList<>();
                    v.forEach((key, value) -> {
                        listaa.add(new OddRefAgencyDto(key, null, value.getOdds()));
                    });
                    otherMap.put("playinfoId", k);
                    otherMap.put("flag", true);
                    otherMap.put("list", listaa);

                } else {
                    otherMap.put("playinfoId", k);
                    otherMap.put("flag", false);
                }
                list.add(otherMap);
                break;
        }
    }


    public void querySscWinInfo(String expect, Integer lotteyType, ResultDto result) {
        result.setData2(lotteyType);
        String nextTime = "";
        String nextExpect = "";
        //获取当前期号,获取下一期的开奖时间
        if (lotteyType == 1) {
            expect = jedis.get(SSCEXPECT);
            nextTime = jedis.get(SSCNEXTTIME);
            nextExpect = jedis.get(SSCNEXTEXPECT);
        } else if (lotteyType == 3) {
            expect = jedis.get(BJSCEXPECT);
            nextTime = jedis.get(BJSCNEXTTIME);
            nextExpect = jedis.get(BJSCNEXTEXPECT);
        }


        // 获取距离开奖时间的时间差
        String dateTime = "";
        long minusSeconds = 0;
        try {

            long time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(nextTime).getTime();
            long currentTimeMillis = System.currentTimeMillis();

            minusSeconds = time - currentTimeMillis;
            SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");// 初始化Formatter的转换格式。
            dateTime = formatter.format(minusSeconds);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //nextExpect
        Lottery lottery = lotteryDao.selectLotteryDetail(expect, lotteyType);
        Map<String, String> map = new HashMap<>();
        map.put("nextOpentime", dateTime);
        map.put("nextexpect", nextExpect);
        if (lottery != null) {
            String ss = lottery.getWinInfo();
            map.put("nowexpect", lottery.getExpect());
            map.put("opencode", lottery.getOpencode());
            map.put("nowWinInfo", ss);
            result.setData(map);
            result.setResultData(true, "拉取信息成功", 0, null);
        } else {
            result.setData(map);
            result.setResultData(true, "还未开奖", 3, null);
        }
    }

    public void queryBetInfo(String expect, Integer userId, Integer lotteyType, ResultDto result) {
        Map<String, Object> params = new HashMap<>();
        params.put("expect", expect);
        params.put("userId", userId);
        params.put("lotteyType", lotteyType);
        List<Bettinginfo> bettinginfos = bettinginfoDao.selectBettinginfoForUser(params);
        List<Map<String, Object>> list = new ArrayList<>();
        for (Bettinginfo bt : bettinginfos) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", bt.getId());//订单号Id
            resultMap.put("expect", bt.getExpect());//期号
            resultMap.put("playwayId", bt.getPlaywayinfoId());//玩法Id
            resultMap.put("betRule", bt.getBettingrule());//下注内容
            resultMap.put("betMoney", bt.getBettingmoney());//下注金额
            resultMap.put("betTime", DateUtil.getFormatDate(bt.getBettingDate(), "HH:mm:ss"));
            list.add(resultMap);
        }
        result.setData2(lotteyType);
        result.setResultData(true, "获取订单信息成功", 0, list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void cancelBet(String ids, Integer userId, ResultDto result) throws Exception {
        boolean flag = true;
        double returnMoney = 0;
        //将ids进行处理
        List<String> list = new ArrayList<>();
        if (ids.contains(",")) {
            String[] strings = ids.split(",");
            for (String ss : strings) {
                list.add(ss);
            }
        } else {
            list.add(ids);
        }
        //删除订单信息,将金额返还
        Set<String> orderIdSet = new HashSet<>();
        for (String aa : list) {
            Bettinginfo bt = bettinginfoDao.selectBetInfoById(aa);
            //验证订单信息
            if (bt != null) {
                if (bt.getState() != 2) {
                    result.setResultData(false, "该订单已经开过了" + bt.getBettingrule(), 2, null);
                } else {
                    //收集不同的订单信息
                    try {
                        orderIdSet.add(bt.getBetOrderID());
                    } catch (Exception e) {
                    }

                    //删除订单信息
                    int i = bettinginfoDao.deleteBetInfoById(aa);
                    //返还金额标识
                    if (i == 1) {
                        returnMoney = DealWithDouble.sum(returnMoney, bt.getBettingmoney(), 2, 4);
                    } else {
                        flag = false;
                        result.setResultData(false, "删除订单信息失败", 3, null);
                    }
                }
            } else {
                result.setResultData(false, "订单不存在", 4, null);
            }
        }

        //返还金额
        if (flag) {
            Account account = accountDao.selectAccountByUserId(userId);
            account.setAccountFunds(DealWithDouble.sum(account.getAccountFunds(), returnMoney, 2, 4));
            accountDao.updateAccountById(account);
            result.setResultData(true, "下注信息取消成功", 0, null);
        }

        //如果这一期信息删除完毕,删除流水表记录
        for (String k : orderIdSet) {
            int a = bettinginfoDao.selectCountByOrderId(k);
            if (a == 0) {
                accountTransactionDao.deleteRecordByOrderId(k);
            }
        }
        ;
    }

    public void  searchPastBetRecord(String startTime, String endTime, Integer userId, Integer lotteyType, ResultDto result) {
        //根据查询出来的天数初始化返回结果
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("userId", userId);
        params.put("lotteyType", lotteyType);
        List<Bettinginfo> bettinginfos = bettinginfoDao.searchPastBetRecord(params);
        if (bettinginfos.size() != 0) {
            if (!("".equals(startTime))) {
                String formatDate = DateUtil.getFormatDate("YYYY-MM-dd");
                if (formatDate.equals(endTime)) {
                    result.setStatus(1);
                } else {
                    result.setStatus(2);
                }
                //今日和昨日的情况
                List<BetRecordDto> list = new ArrayList<>();
                BetRecordDto btResult = null;

                for (Bettinginfo bt : bettinginfos) {
                    if (bt.getState() == 2) {
                        continue;
                    }
                    btResult = new BetRecordDto();
                    if (bt.getState() == 1) {
                        //李代桃僵
                        btResult.setRemark(bt.getPlaywayName());
                        btResult.setBetMoney(Double.parseDouble(bt.getPlaywayName()));
                    } else if (bt.getState() == 0) {
                        btResult.setRemark("未中奖");
                        btResult.setBetMoney(bt.getAccountChange());
                    } else if (bt.getState() == 2) {
                        btResult.setRemark("未开奖");
                        btResult.setBetMoney(bt.getAccountChange());
                    }

                    btResult.setBetRule(bt.getBettingrule());
                    btResult.setBetTime(DateUtil.getFormatDate(bt.getPayDate(), "HH:mm"));
                    if (lotteyType == 1) {
                        btResult.setExpect(bt.getExpect().substring(4));
                    } else {
                        btResult.setExpect(bt.getExpect());
                    }
                    btResult.setPlaywayId(bt.getPlaywayinfoId());
                    btResult.setId(bt.getId());
                    list.add(btResult);
                }
                result.setData2(lotteyType);
                result.setResultData(true, "查询记录成功", 0, list);
            } else {
                List<String> days = bettinginfoDao.selectDays(params);
                Map<String, List<BetRecordDto>> map = new HashMap<>();
                for (String ss : days) {
                    List<BetRecordDto> list = new ArrayList<>();
                    map.put(ss, list);
                }
                for (Bettinginfo bt : bettinginfos) {
                    if (bt.getState() == 2) {
                        continue;
                    }
                    BetRecordDto btResult = new BetRecordDto();
                    if (bt.getState() == 1) {
                        //李代桃僵
                        btResult.setRemark(bt.getPlaywayName());
                    } else if (bt.getState() == 0) {
                        btResult.setRemark("未中奖");
                    } else if (bt.getState() == 2) {
                        btResult.setRemark("未开奖");
                    }
                    btResult.setBetMoney(bt.getBettingmoney());
                    btResult.setBetRule(bt.getBettingrule());
                    btResult.setBetTime(DateUtil.getFormatDate(bt.getPayDate(), "HH:mm"));
                    if (lotteyType == 1) {
                        btResult.setExpect(bt.getExpect().substring(4));
                    } else {
                        btResult.setExpect(bt.getExpect());
                    }
                    btResult.setPlaywayId(bt.getPlaywayinfoId());
                    btResult.setId(bt.getId());
                    String formatDate = DateUtil.getFormatDate(bt.getBettingDate(), "YYYY-MM-dd");
                    map.get(formatDate).add(btResult);
                }
                ;
                List<Map<String, Object>> mapList = new ArrayList<>();
                map.forEach((k, v) -> {
                    Map<String, Object> aa = new HashMap<>();
                    aa.put("date", k);
                    aa.put("list", v);
                    mapList.add(aa);
                });
                result.setData2(lotteyType);
                result.setStatus(3);
                result.setResultData(true, "查询成功", 0, mapList);
            }
        } else {
            result.setData2(lotteyType);
            result.setResultData(false, "当前无下注信息", 0, null);
        }
    }

    public void serachForMouthCount(Integer userId, Integer lotteyType, ResultDto result) throws Exception {
        //获取系统时间,得到类似("2018-06%")的结果
        String formatDate = DateUtil.getFormatDate("YYYY-MM") + "%";
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("time", formatDate);
        params.put("lotteyType", lotteyType);
        List<BetDto> list = bettinginfoDao.serachForMouthCount(params);
        List<LinkedHashMap<String, Object>> lists = new ArrayList<>();
        list.forEach((k) -> {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            map.put("date", k.getBetTime());
            map.put("betMoney", DealWithDouble.sum(k.getBetMoney(), 0, 2, 4));
            map.put("winMoney", DealWithDouble.sum(k.getWinMoney(), 0, 2, 4));
            map.put("profit", DealWithDouble.sub(k.getWinMoney(), k.getBetMoney(), 2, 4));
            lists.add(map);
        });
        result.setData2(lotteyType);
        result.setResultData(true, "查询数据成功", 0, lists);
    }

    public void searchAllbetRecordByUserID(Integer userId, Integer lotteyType, Integer pageNow, Integer pageSize, ResultDto result) throws Exception {
        int count = bettinginfoDao.selectRecordCount(userId, lotteyType);
        PageDto page = new PageDto();
        page.setTotalCount(count);
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("lotteyType", lotteyType);
        params.put("userId", userId);
        List<Bettinginfo> bettinginfos = bettinginfoDao.searchAllbetRecordByUserID(params);
        List<LinkedHashMap<String, Object>> list = new ArrayList<>();
        result.setData2(lotteyType);
        if (bettinginfos.size() > 0) {
            bettinginfos.forEach((k) -> {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("time", DateUtil.getFormatDate(k.getBettingDate(), "MM.dd HH:mm"));
                map.put("remark", k.getRemark());
                map.put("moneyChange", k.getAccountChange());
                map.put("count", k.getAccountMoney());
                list.add(map);
            });
            result.setData(page);
            result.setResultData(true, "拉取信息成功", 0, list);
        } else {
            result.setResultData(false, "nodata", 2, null);
        }

    }

    public void searchBetZoushi(Integer lotteyType, Integer pageSize, ResultDto result) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("lotteyType", lotteyType);
        params.put("pageSize", pageSize);
        List<Lottery> lotteries = lotteryDao.searchBetZoushi(params);
        List<Map<String, Object>> list = new ArrayList<>();
        lotteries.forEach((k) -> {
            Map<String, Object> map = new HashMap<>();
            String sp = k.getOpencode();
            //34,大,双,C&&虎,对子,对子,半顺
            String winInfo = k.getWinInfo();
            String[] strings = winInfo.split("&&");
            if (lotteyType == 1) {
                map.put("expect", k.getExpect().substring(4));
                map.put("opencode", sp.split(","));
                map.put("show1", strings[1].substring(0, 1));
                map.put("show2", strings[0].split(","));
                map.put("show3", strings[1].substring(2).replace(",", "/"));
            } else if (lotteyType == 3) {
                map.put("expect", k.getExpect());
                List<String> list1 = new ArrayList<>();
                String[] split = sp.split(",");
                for (String ss : split) {
                    if ("10".equals(ss)) {
                        list1.add(ss);
                    } else {
                        list1.add(ss.substring(1));
                    }
                }
                map.put("opencode", list1);
                map.put("show1", strings[0].split(","));
                map.put("show2", strings[1].split(","));
            }
            list.add(map);
        });
        result.setData2(lotteyType);
        result.setResultData(true, "拉取信息成功", 0, list);
    }

    public void searchSscChanglong(Integer lotteyType, ResultDto result) throws Exception {
        result.setData2(lotteyType);
        if (lotteyType == 1) {
            getSscChanglong(result);
        } else if (lotteyType == 3) {
            getBjscChanglong(result);
        }

    }

    private void getBjscChanglong(ResultDto result) throws Exception {
        LinkedList<Changlong> linkedList = InitData.BJSC_SORT_CHANGLONG;
        List<Map<String, Object>> list = new ArrayList<>();
        if (linkedList.size() > 0) {
            for (int i = 0; i < 30; i++) {
                Changlong k = linkedList.get(i);
                Map<String, Object> map = new HashMap<>();
                String[] split = k.getBetRule().split("/");
                if (split.length > 1) {
                    map.put("number", split[0]);
                    map.put("bet", split[1]);
                    map.put("lianqi", k.getContinueNumber());
                } else {
                    map.put("number", "和");
                    map.put("bet", split[0]);
                    map.put("lianqi", k.getContinueNumber());
                }
                list.add(map);
            }
            result.setResultData(true, "拉取北京赛车长龙信息成功", 0, list);
        } else {
            result.setResultData(false, "还未形成长龙信息", 2, null);
        }
    }


    //北京赛车下注
    public void bjscPlayWay(Integer userId, Integer lotteyType, String agencyMark, List<PlaywayDto> list, Integer loginType, ResultDto result) throws Exception {
        //先决条件判断
        BetConditionDto bto = new BetConditionDto(userId, agencyMark, result, lotteyType, list);
        bjscPlayUtil.getInstance(bto, loginType);
        if (result.isSuccess()) {
            if (loginType == 0) {
                bjscPlayUtil.insertBetData(list, userId, lotteyType, agencyMark, result);
            } else {
                bjscPlayUtil.insertTourBetData(list, userId, lotteyType, agencyMark, result);
            }

        }
    }

    private void getSscChanglong(ResultDto result) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        LinkedList<Changlong> linkedList = InitData.SSC_SORT_CHANGLONG;
        if (linkedList.size() > 0) {
            for (int i = 0; i < 14; i++) {
                Map<String, Object> map = new HashMap<>();
                Changlong ss = linkedList.get(i);
                String betRule = ss.getBetRule();
                if (betRule.length() > 1) {
                    if (betRule.contains("总")) {
                        map.put("number", "和");
                        map.put("bet", betRule.split("/")[1]);
                        map.put("lianqi", ss.getContinueNumber());
                        list.add(map);
                    } else {
                        String[] aa = betRule.split("/");
                        map.put("number", aa[0]);
                        map.put("bet", aa[1]);
                        map.put("lianqi", ss.getContinueNumber());
                        list.add(map);
                    }
                } else {
                    if (betRule.contains("A") || betRule.contains("B") || betRule.contains("C")) {
                        map.put("number", "和");
                        map.put("bet", betRule);
                        map.put("lianqi", ss.getContinueNumber());
                        list.add(map);
                    } else {
                        map.put("number", "龙虎");
                        map.put("bet", betRule);
                        map.put("lianqi", ss.getContinueNumber());
                        list.add(map);
                    }
                }
            }
            result.setResultData(true, "success", 0, list);
        } else {
            result.setResultData(false, "no date", 2, null);
        }
    }


}
